package com.unittec.drainage.provider.auth.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.unittec.drainage.provider.auth.entity.ExportTaskConfigEntity;
import com.unittec.drainage.provider.auth.entity.ImportTaskConfigEntity;
import com.unittec.drainage.provider.auth.service.ExportTaskConfigService;
import com.unittec.drainage.provider.auth.service.ImportTaskConfigService;
import com.unittec.drainage.provider.log.Log;
import com.unittec.drainage.provider.utils.ValidationUtils;
import com.unittec.drainage.sdk.auth.domain.ExportTaskConfig;
import com.unittec.drainage.sdk.auth.domain.ImportTaskConfig;
import com.unittec.drainage.sdk.auth.request.Req101901;
import com.unittec.drainage.sdk.auth.request.Req101902;
import com.unittec.drainage.sdk.auth.request.Req101903;
import com.unittec.drainage.sdk.auth.request.Req101904;
import com.unittec.drainage.sdk.auth.request.Req101905;
import com.unittec.drainage.sdk.auth.request.Req101906;
import com.unittec.drainage.sdk.auth.response.Res101901;
import com.unittec.drainage.sdk.auth.response.Res101902;
import com.unittec.drainage.sdk.auth.response.Res101903;
import com.unittec.drainage.sdk.auth.response.Res101904;
import com.unittec.drainage.sdk.auth.response.Res101905;
import com.unittec.drainage.sdk.auth.response.Res101906;
import com.unittec.drainage.sdk.log.enums.LogOperateEnum;
import com.unittec.drainage.sdk.log.enums.LogStatusEnum;
import com.unittec.drainage.sdk.root.domain.GatewayRequest;
import com.unittec.drainage.sdk.root.domain.Result;
import com.unittec.drainage.sdk.root.enums.BizCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description:
 * @Author: wuxinwei
 * @CreateDate: 2019/11/18 11:09
 */
@RestController
@Api(tags = "EXCEL导入导出任务管理")
public class ExcelImportAndExportController {

    @Autowired
    Log log;

    @Autowired
    private ExportTaskConfigService exportTaskConfigService;

    @Autowired
    private ImportTaskConfigService importTaskConfigService;

    @RequestMapping(value = "/101901", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增导出任务", notes = "新增导出任务")
    public Result<Res101901> addExportTask(@RequestBody GatewayRequest<Req101901> req) {
        log.info(req, "addExportTask  {}  ", "begin");
        long startLogTime = System.currentTimeMillis();

        try {
            ValidationUtils.validateEntityStr(req.getContent());
            ExportTaskConfigEntity entity = new ExportTaskConfigEntity();
            BeanUtils.copyProperties(req.getContent().getExportTaskConfig(), entity);
            exportTaskConfigService.save(entity);
            log.addOperateLog("101901", JSON.toJSONString(req.getContent()), "新增导出任务", "新增导入任务",
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            log.info(req, "addExportTask  {}  ", "end");

            Res101901 res = new Res101901();
            res.setExportTaskConfig(req.getContent().getExportTaskConfig());
            return Result.success(req, res);
        } catch (BeansException e) {
            log.addOperateLog("101901", JSON.toJSONString(req.getContent()), "新增导出任务失败", log.buildErrmsg(e),
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            // 调用微服务数据保存异常
            log.error(req, BizCode.QUERY_ERROR.getMessage(), e);
            return Result.error(req, BizCode.QUERY_ERROR.getCode(), BizCode.QUERY_ERROR.getMessage());
        }
    }

    @RequestMapping(value = "/101902", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改导出任务", notes = "修改导入任务")
    public Result<Res101902> updateExportTask(@RequestBody GatewayRequest<Req101902> req) {
        log.info(req, "updateExportTask  {}  ", "begin");
        long startLogTime = System.currentTimeMillis();

        try {
            ValidationUtils.validateEntityStr(req.getContent());
            ExportTaskConfigEntity entity = new ExportTaskConfigEntity();
            BeanUtils.copyProperties(req.getContent().getExportTaskConfig(), entity);
            exportTaskConfigService.updateById(entity);
            log.addOperateLog("101902", JSON.toJSONString(req.getContent()), "修改导出任务", "修改导入任务",
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            log.info(req, "updateExportTask  {}  ", "end");

            Res101902 res = new Res101902();
            res.setExportTaskConfig(req.getContent().getExportTaskConfig());
            return Result.success(req, res);
        } catch (BeansException e) {
            log.addOperateLog("101902", JSON.toJSONString(req.getContent()), "修改导出任务失败", log.buildErrmsg(e),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            // 调用微服务数据保存异常
            log.error(req, BizCode.SAVE_ERROR.getMessage(), e);
            return Result.error(req, BizCode.SAVE_ERROR.getCode(), BizCode.SAVE_ERROR.getMessage());
        }
    }

    @RequestMapping(value = "/101903", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取导出任务详情", notes = "获取导出任务详情")
    public Result<Res101903> getExportTaskById(@RequestBody GatewayRequest<Req101903> req) {
        log.info(req, "getExportTaskById  {}  ", "begin");
        long startLogTime = System.currentTimeMillis();

        try {
            ValidationUtils.validateEntityStr(req.getContent());
            ExportTaskConfig exportTaskConfig = new ExportTaskConfig();

            ExportTaskConfigEntity entity = exportTaskConfigService.getById(req.getContent().getId());

            if (entity != null) {
                BeanUtils.copyProperties(entity, exportTaskConfig);
            }
            Res101903 res = new Res101903();
            res.setExportTaskConfig(exportTaskConfig);
            log.addOperateLog("101903", JSON.toJSONString(req.getContent()), "获取导出任务详情", "获取导入任务详情",
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            log.info(req, "getExportTaskById  {}  ", "end");
            return Result.success(req, res);
        } catch (BeansException e) {
            log.addOperateLog("101903", JSON.toJSONString(req.getContent()), "获取导出任务详情失败", log.buildErrmsg(e),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            // 调用微服务数据保存异常
            log.error(req, BizCode.SAVE_ERROR.getMessage(), e);
            return Result.error(req, BizCode.SAVE_ERROR.getCode(), BizCode.SAVE_ERROR.getMessage());
        }
    }

    @RequestMapping(value = "/101904", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增导入任务", notes = "新增导入任务")
    public Result<Res101904> addImportTask(@RequestBody GatewayRequest<Req101904> req) {
        log.info(req, "addImportTask  {}  ", "begin");
        long startLogTime = System.currentTimeMillis();

        try {
            ValidationUtils.validateEntityStr(req.getContent());
            ImportTaskConfigEntity entity = new ImportTaskConfigEntity();
            BeanUtils.copyProperties(req.getContent().getImportTaskConfig(), entity);
            importTaskConfigService.save(entity);
            log.addOperateLog("101904", JSON.toJSONString(req.getContent()), "新增导入任务", "新增导出任务",
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            log.info(req, "addImportTask  {}  ", "end");

            Res101904 res = new Res101904();
            res.setImportTaskConfig(req.getContent().getImportTaskConfig());
            return Result.success(req, res);
        } catch (BeansException e) {
            log.addOperateLog("101904", JSON.toJSONString(req.getContent()), "新增导入任务失败", log.buildErrmsg(e),
                LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            // 调用微服务数据保存异常
            log.error(req, BizCode.SAVE_ERROR.getMessage(), e);
            return Result.error(req, BizCode.SAVE_ERROR.getCode(), BizCode.SAVE_ERROR.getMessage());
        }
    }

    @RequestMapping(value = "/101905", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改导入任务", notes = "修改导入任务")
    public Result<Res101905> updateImportTask(@RequestBody GatewayRequest<Req101905> req) {
        log.info(req, "updateImportTask  {}  ", "begin");
        long startLogTime = System.currentTimeMillis();

        try {
            ValidationUtils.validateEntityStr(req.getContent());
            ImportTaskConfigEntity entity = new ImportTaskConfigEntity();
            BeanUtils.copyProperties(req.getContent().getImportTaskConfig(), entity);
            importTaskConfigService.updateById(entity);
            log.addOperateLog("101905", JSON.toJSONString(req.getContent()), "修改导入任务", "修改导出任务",
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            log.info(req, "updateImportTask  {}  ", "end");
            Res101905 res = new Res101905();
            res.setImportTaskConfig(req.getContent().getImportTaskConfig());
            return Result.success(req, res);
        } catch (BeansException e) {
            log.addOperateLog("101905", JSON.toJSONString(req.getContent()), "修改导入任务失败", log.buildErrmsg(e),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            // 调用微服务数据保存异常
            log.error(req, BizCode.SAVE_ERROR.getMessage(), e);
            return Result.error(req, BizCode.SAVE_ERROR.getCode(), BizCode.SAVE_ERROR.getMessage());
        }
    }

    @RequestMapping(value = "/101906", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取导入任务详情", notes = "获取导出任务详情")
    public Result<Res101906> getImportTaskById(@RequestBody GatewayRequest<Req101906> req) {
        log.info(req, "updateImportTask  {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            ImportTaskConfig exportTaskConfig = new ImportTaskConfig();
            ImportTaskConfigEntity entity = importTaskConfigService.getById(req.getContent().getId());
            if (entity != null) {
                BeanUtils.copyProperties(entity, exportTaskConfig);
            }
            Res101906 res = new Res101906();
            res.setImportTaskConfig(exportTaskConfig);
            log.addOperateLog("101906", JSON.toJSONString(req.getContent()), "获取导入任务详情", "获取导出任务详情",
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            log.info(req, "getImportTaskById  {}  ", "end");
            return Result.success(req, res);
        } catch (BeansException e) {
            log.addOperateLog("101906", JSON.toJSONString(req.getContent()), "获取导入任务详情失败", log.buildErrmsg(e),
                LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            // 调用微服务数据保存异常
            log.error(req, BizCode.QUERY_ERROR.getMessage(), e);
            return Result.error(req, BizCode.QUERY_ERROR.getCode(), BizCode.QUERY_ERROR.getMessage());
        }
    }

}
